<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="java.net.URLDecoder"%>
<%@page import="java.io.UnsupportedEncodingException"%>
<%@page import="java.util.Date"%>
<%@page import="java.io.File"%>
<%@ page contentType="text/html; charset=UTF-8" %>

<%!private final static int languageNo=1; //语言版本，0 : 中文； 1：英文
String[] strCurrentFolder= {"当前目录","Current Folder"};
String[] strDrivers      = {"驱动器","Drivers"};
String[] strFileName     = {"文件名称","File Name"};
String[] strFileSize     = {"文件大小","File Size"};
String[] strLastModified = {"最后修改","Last Modified"};
String[] strFileOperation= {"文件操作","Operations"};
String[] strFileEdit     = {"前往","Go"};
String[] strFileDown     = {"下载","Download"};
String[] strFileSource     = {"源码","source"};
String[] strFileDel      = {"源码","source"};
String error = "";  
	String getDrivers() {// 列出可用的文件系统根 基本上linux是  /  windows是  C:\   D:\   F:\ 
		StringBuffer sb = new StringBuffer("");
		File roots[] = File.listRoots();
		for (int i = 0; i < roots.length; i++) {
			sb.append("<a href='?path=" + roots[i] + "'>");
			sb.append(roots[i] + "</a> &nbsp;&nbsp;");
		}
		return sb.toString();
	}

	String formatPath(String p) {//将一个\(转义\\)转换成两个\\  只是windows上的路径转义 linux上是 / 无需改变
		StringBuffer sb = new StringBuffer();
/* 		for (int i = 0; i < p.length(); i++) {
			if (p.charAt(i) == '\\') {
				sb.append("\\\\");
			} else {
				sb.append(p.charAt(i));
			}
		}
		String str = sb.toString(); */
		String str = p;
	
		try {
 			str = URLEncoder.encode(str, "UTF-8");
			str = URLEncoder.encode(str, "UTF-8"); 
		} catch (UnsupportedEncodingException e) {
			error += "<br>" + e.toString();
			e.printStackTrace();
		}  
		return str;

	}

%>
<% 
    error = "";

    //request.getRealPath("/");
    String rootPath=request.getSession().getServletContext().getRealPath("/");

	String strDir = request.getParameter("path");
	if (strDir == null || strDir.length() < 1) {
		//strDir = request.getRealPath("/");//警告消除强迫症 所以调用下面的
		strDir = rootPath;
	}else{
		try {
			strDir = URLDecoder.decode(strDir, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			error += "<br>" + e.toString();
			e.printStackTrace();
		}
	}

	StringBuffer sb = new StringBuffer("");
	StringBuffer sbFile = new StringBuffer("");
	try {

		File objFile = new File(strDir);
		File list[] = objFile.listFiles();
		if (objFile.getAbsolutePath().length() > 3) {
			sb.append("<a href='?path="
					+ formatPath(objFile.getParentFile().getAbsolutePath()) + "'>");
			sb.append("To ParentFile</a><br>- - - -<br>");
		}
		for (int i = 0; i < list.length; i++) {
			if (list[i].isDirectory()) { 
				sb.append("<a href='?path="
						+ formatPath(list[i].getAbsolutePath()) + "'>"
						+ list[i].getName() + "</a><br>");
			} else {
				String strLen = "";
				String strDT = "";
				String strUnit = "";
				double lFile = 0;
				lFile = list[i].length();

				
				if (lFile > (1024*1024*1024)) {//GB
					lFile = lFile / (1024*1024*1024);
					strUnit = "G";
				}else if (lFile > (1024*1024)) {//MB
					lFile = lFile / (1024*1024);
					strUnit = "M";
				} else if (lFile > 1024) {//KB
					lFile = lFile / 1024;
					strUnit = "K";
				} else {//Byte
					strUnit = "Byte";
				}
				lFile = Math.round(lFile*100)/100.0;
				
				strLen = "" + lFile + " "+strUnit;
				
				Date dt = new Date(list[i].lastModified());
				
				
				SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
				
				strDT = df.format(dt);
 
				
				StringBuffer fileUrl = new StringBuffer(list[i].getPath().replace("\\", "/"));
				 
				fileUrl.delete(0, rootPath.length());
 
				sbFile.append("<tr><td>");
				sbFile.append("" + list[i].getName());
				sbFile.append("</td><td>");
				sbFile.append("" + strLen);
				sbFile.append("</td><td>");
				sbFile.append("" + strDT);
				sbFile.append("</td><td>");
				sbFile.append(" <a  target='_blank' href='"+request.getContextPath()+"/"+fileUrl+"'>");
				sbFile.append(strFileEdit[languageNo] + "</a> ");
				sbFile.append(" <a href='fileDown.jsp?path="+formatPath(list[i].getAbsolutePath())+"'>");
				sbFile.append(strFileDown[languageNo] + "</a> ");
				sbFile.append(" <a target='_blank' href='fileSource.jsp?&path="+formatPath(list[i].getAbsolutePath())+"'>");
				sbFile.append(strFileSource[languageNo]+"</a> ");
		/* 		sbFile.append(" <a href=>");
				sbFile.append(strFileDel[languageNo]+"</a> "); */
				sbFile.append("</td></tr>\r\n");
			}
		}
	} catch (Exception e) {
		e.printStackTrace();
		error += "<br>" + e.toString();
	}
%>
<!DOCTYPE>
<html>
<head>
<style type="text/css">
 

</style>
</head>
<body>
<span style="color: red;"> 
SOS!此文件仅学习参考用,勿在生产环境,非公开项目中使用。
<%=error %>
</span>


<table>
<tr>
  <td>language</td>
  <td>
    <select><option>English</option><option>简体中文</option></select>
  </td>
</tr>
<tr>
<td><%=strDrivers[languageNo] %>:<br><%=getDrivers()%></td>
<td><%=strCurrentFolder[languageNo]%>:<br><%=strDir %></td>
</tr>
<!-- </table>
<table> -->
<tr>
<td valign="top">
 <%=sb%> 
</td>
<td valign="top">
<table style="min-width: 800px;">
<tr>
<td width="20%"><%=strFileName[languageNo] %></td>
<td width="15%"><%=strFileSize[languageNo] %></td>
<td width="30%"><%=strLastModified[languageNo] %></td>
<td width="35%"><%=strFileOperation[languageNo] %></td>
</tr>
<%=sbFile %> 
</table>
</td>
</tr>
</table>
</body>
</html>